<?php

$oTpl = &App::getTemplate();

$oDb = &Database::get();

$oReq = &App::getRequest();

$sType = $oReq->get('type');
if ( $sType )
{
	$aGetChar = explode("_", $sType);
	$sCharSet = $aGetChar[0];
	$sCollate = $sType;
}

require_once App::getParam('path') . 'include/settings/backup/mysql.structure.php';

$aSupportedCollations = $oDb->getRows("SHOW COLLATION");

$aSupportedCharsets = array();
foreach($aSupportedCollations as  $aColla)
{
	$aSupportedCharsets[$aColla['Charset']][$aColla['Collation']] = $aColla['Collation'];
}

$aCachedTables = array();
foreach($aCol as $sTable => $sTableValues)
{
	$aCachedTables[] = App::getParam('db', 'prefix') . $sTable;
}

$aTables = $oDb->getRows("SHOW TABLES");

$aItems = array();
foreach($aTables as $iKey => $aTable)
{
	$sCacheName = 'Tables_in_'. (ereg('WIN', PHP_OS) ? strtolower(App::getParam('db', 'name')) : App::getParam('db', 'name')) .'';
	if ( in_array($aTable[$sCacheName], $aCachedTables) )
	{
		if ( $sType )
		{
			$oDb->query("ALTER TABLE `". $aTable[$sCacheName] ."`  DEFAULT CHARACTER SET ". $sCharSet ." COLLATE ". $sCollate ."");
		}

		$aFields = $oDb->getRows("SHOW FULL FIELDS FROM ". $aTable[$sCacheName] ."");

		$aCollations = $oDb->getRows("SHOW TABLE STATUS FROM `". App::getParam('db', 'name') ."`");

		$aCachedStatus = array();
		foreach($aCollations as $aStatus)
		{
			if ( in_array($aStatus['Name'], $aCachedTables) )
			{
				$aCachedStatus[$aStatus['Name']] = $aStatus['Collation'];
			}
		}

		$aItems[$iKey]['Table'] = array('Name' => $aTable[$sCacheName], 'Collation' => $aCachedStatus[$aStatus['Name']]);

		foreach($aFields as $iFieldId => $aItem)
		{
			if ( !empty($aItem['Collation']) && $aItem['Collation'] != 'NULL' )
			{
				if ( $sType )
				{
					$oDb->query("ALTER TABLE `". $aTable[$sCacheName] ."` CHANGE `". $aItem['Field'] ."` `". $aItem['Field'] ."` ". strtoupper($aItem['Type']) ." CHARACTER SET ". $sCharSet ." COLLATE ". $sCollate ." NOT NULL");
				}

				$aItems[$iKey]['Detail'][] = array('Field' => $aItem['Field'], 'Collation' => $aItem['Collation']);
			}
		}
	}
}

if ( $sType )
{
	App::gotoUrl('admin.language.collation', array(), App::format('Mod_Language.collation_updated'));
}

$oTpl->assign(array
(
	'aItems' => $aItems,
	'aColl' => $aSupportedCharsets
));

?>